package cn.tedu.ymg.product.service.impl;

import cn.tedu.ymg.commons.ex.ServiceException;
import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.util.PageInfoToPageDataConverter;
import cn.tedu.ymg.commons.web.ServiceCode;
import cn.tedu.ymg.product.mapper.HappyMapper;
import cn.tedu.ymg.product.pojo.entity.Happy;
import cn.tedu.ymg.product.pojo.param.HappyAddNewParam;
import cn.tedu.ymg.product.pojo.param.HappyUpdateInfoParam;
import cn.tedu.ymg.product.pojo.vo.HappyListItemVO;
import cn.tedu.ymg.product.pojo.vo.HappyStandardVO;
import cn.tedu.ymg.product.service.IHappyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class HappyServiceImpl implements IHappyService {

    @Autowired
    HappyMapper happyMapper;



    @Override
    public void addNew(HappyAddNewParam happyAddNewParam) {

        log.debug("开始处理【添加喜讯的业务】，参数", happyAddNewParam);

        //将喜讯写入到数据库中
        Happy Happy = new Happy();
        BeanUtils.copyProperties(happyAddNewParam, Happy);

        int rows = happyMapper.insert(Happy);
        if (rows !=1){
            String Happy1 = "添加喜讯失败，服务器忙，请稍后再试！";
            log.warn(Happy1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Happy1);
        }
        log.debug("将新的喜讯数据写入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除喜讯】的业务，参数：{}", id);
        Happy Happy = happyMapper.selectById(id);
        if (Happy ==null){
            String Happy1 = "删除喜讯失败，Id不存在";
            log.warn(Happy1);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Happy1);
        }

        int rows = happyMapper.deleteById(id);
        if (rows!=1){
            String Happy1 = "删除喜讯失败，服务器忙，请稍后再试！";
            log.warn(Happy1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Happy1);
        }
        log.debug("从数据库删除喜讯，完成！");
    }

    @Override
    public void updateInfoById(Long id, HappyUpdateInfoParam happyUpdateInfoParam) {

        log.debug("开始处理【修改喜讯】的业务，ID:{},新数据：{}", id, happyUpdateInfoParam);

        //检查喜讯是否存在，如果不存在，则抛出异常
        QueryWrapper<Happy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = happyMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的喜讯数量，结果：{}", countById);
        if (countById == 0) {
            String Happy = "修改喜讯失败，喜讯数据不存在！";
            log.warn(Happy);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Happy);
        }

        //执行修改
        Happy Happy = new Happy();
        BeanUtils.copyProperties(happyUpdateInfoParam, Happy);
        Happy.setId(id);
        int rows = happyMapper.updateById(Happy);
        if (rows!=1){
            String Happy1 = "修改喜讯失败，服务器忙，请稍后再试！";
            log.warn(Happy1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Happy1);
        }
        log.debug("从数据库修改喜讯，完成！");
    }

    @Override
    public HappyStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询喜讯】的业务，参数：{}", id);
        HappyStandardVO queryResult = happyMapper.getStandardById(id);
        if (queryResult==null){
            String Happy = "查询喜讯失败，喜讯数据不存在！";
            log.warn(Happy);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,Happy);
        }
        return queryResult;
    }

    @Override
    public PageData<HappyListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<HappyListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询喜讯列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<HappyListItemVO> list =  happyMapper.list();
        PageInfo<HappyListItemVO> pageInfo = new PageInfo<>(list);
        PageData<HappyListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
